import Vue from "vue";
import VueRouter from "vue-router";
import { isLogined } from "../utils/tools";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: () => import("../views/Home"),
  },
  {
    path: "/Music",
    name: "Music",
    component: () => import("../views/Music.vue"),
  },

  {
    path: "/user",
    name: "User",
    component: () => import("../views/User.vue"),
  },

  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login.vue"),
    meta: {
      hideNav: true,
    },
  },
  {
    path: "/reg",
    name: "Reg",
    component: () => import("../views/Reg.vue"),
    meta: {
      hideNav: true,
    },
  },
  {
    path: "/search",
    name: "Search",
    component: () => import("../views/search.vue"),
  },
  {
    path: "/bSearch",
    name: "bSearch",
    component: () => import("../views/b-search.vue"),
    meta: {
      hideNav: true,
    },
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    if (isLogined()) {
      next();
    } else {
      next({
        name: "Login",
      });
    }
  } else {
    next();
  }
});
export default router;
